package lego.diagram.part;

import lego.LegoPackage;
import lego.Root;
import lego.diagram.edit.parts.ActionerPortEditPart;
import lego.diagram.edit.parts.LightSensorEditPart;
import lego.diagram.edit.parts.LightSensorNameEditPart;
import lego.diagram.edit.parts.MicroSensorEditPart;
import lego.diagram.edit.parts.MicroSensorNameEditPart;
import lego.diagram.edit.parts.MotorActionerEditPart;
import lego.diagram.edit.parts.MotorActionerNameEditPart;
import lego.diagram.edit.parts.PicoNetEditPart;
import lego.diagram.edit.parts.RobotEditPart;
import lego.diagram.edit.parts.RobotNameEditPart;
import lego.diagram.edit.parts.RobotRobotFigureCompartmentEditPart;
import lego.diagram.edit.parts.RootEditPart;
import lego.diagram.edit.parts.SensorPortEditPart;
import lego.diagram.edit.parts.TouchSensorEditPart;
import lego.diagram.edit.parts.TouchSensorNameEditPart;
import lego.diagram.edit.parts.UltraSoundSensorEditPart;
import lego.diagram.edit.parts.UltraSoundSensorNameEditPart;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.structure.DiagramStructure;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class LegoVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "lego.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (RootEditPart.MODEL_ID.equals(view.getType())) {
				return RootEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return lego.diagram.part.LegoVisualIDRegistry.getVisualID(view
				.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				LegoDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (LegoPackage.eINSTANCE.getRoot().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((Root) domainElement)) {
			return RootEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = lego.diagram.part.LegoVisualIDRegistry
				.getModelID(containerView);
		if (!RootEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (RootEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = lego.diagram.part.LegoVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = RootEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case RootEditPart.VISUAL_ID:
			if (LegoPackage.eINSTANCE.getMicroSensor().isSuperTypeOf(
					domainElement.eClass())) {
				return MicroSensorEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getPicoNet().isSuperTypeOf(
					domainElement.eClass())) {
				return PicoNetEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getUltraSoundSensor().isSuperTypeOf(
					domainElement.eClass())) {
				return UltraSoundSensorEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getTouchSensor().isSuperTypeOf(
					domainElement.eClass())) {
				return TouchSensorEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getLightSensor().isSuperTypeOf(
					domainElement.eClass())) {
				return LightSensorEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getRobot().isSuperTypeOf(
					domainElement.eClass())) {
				return RobotEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getMotorActioner().isSuperTypeOf(
					domainElement.eClass())) {
				return MotorActionerEditPart.VISUAL_ID;
			}
			break;
		case RobotRobotFigureCompartmentEditPart.VISUAL_ID:
			if (LegoPackage.eINSTANCE.getActionerPort().isSuperTypeOf(
					domainElement.eClass())) {
				return ActionerPortEditPart.VISUAL_ID;
			}
			if (LegoPackage.eINSTANCE.getSensorPort().isSuperTypeOf(
					domainElement.eClass())) {
				return SensorPortEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = lego.diagram.part.LegoVisualIDRegistry
				.getModelID(containerView);
		if (!RootEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (RootEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = lego.diagram.part.LegoVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = RootEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case RootEditPart.VISUAL_ID:
			if (MicroSensorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PicoNetEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (UltraSoundSensorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (TouchSensorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (LightSensorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RobotEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (MotorActionerEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case MicroSensorEditPart.VISUAL_ID:
			if (MicroSensorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case UltraSoundSensorEditPart.VISUAL_ID:
			if (UltraSoundSensorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case TouchSensorEditPart.VISUAL_ID:
			if (TouchSensorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case LightSensorEditPart.VISUAL_ID:
			if (LightSensorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RobotEditPart.VISUAL_ID:
			if (RobotNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (RobotRobotFigureCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case MotorActionerEditPart.VISUAL_ID:
			if (MotorActionerNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RobotRobotFigureCompartmentEditPart.VISUAL_ID:
			if (ActionerPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SensorPortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(Root element) {
		return true;
	}

	/**
	 * @generated
	 */
	public static boolean checkNodeVisualID(View containerView,
			EObject domainElement, int candidate) {
		if (candidate == -1) {
			//unrecognized id is always bad
			return false;
		}
		int basic = getNodeVisualID(containerView, domainElement);
		return basic == candidate;
	}

	/**
	 * @generated
	 */
	public static boolean isCompartmentVisualID(int visualID) {
		switch (visualID) {
		case RobotRobotFigureCompartmentEditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static boolean isSemanticLeafVisualID(int visualID) {
		switch (visualID) {
		case RootEditPart.VISUAL_ID:
			return false;
		case MicroSensorEditPart.VISUAL_ID:
		case PicoNetEditPart.VISUAL_ID:
		case UltraSoundSensorEditPart.VISUAL_ID:
		case TouchSensorEditPart.VISUAL_ID:
		case LightSensorEditPart.VISUAL_ID:
		case MotorActionerEditPart.VISUAL_ID:
		case ActionerPortEditPart.VISUAL_ID:
		case SensorPortEditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static final DiagramStructure TYPED_INSTANCE = new DiagramStructure() {
		/**
		 * @generated
		 */
		@Override
		public int getVisualID(View view) {
			return lego.diagram.part.LegoVisualIDRegistry.getVisualID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public String getModelID(View view) {
			return lego.diagram.part.LegoVisualIDRegistry.getModelID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public int getNodeVisualID(View containerView, EObject domainElement) {
			return lego.diagram.part.LegoVisualIDRegistry.getNodeVisualID(
					containerView, domainElement);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean checkNodeVisualID(View containerView,
				EObject domainElement, int candidate) {
			return lego.diagram.part.LegoVisualIDRegistry.checkNodeVisualID(
					containerView, domainElement, candidate);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isCompartmentVisualID(int visualID) {
			return lego.diagram.part.LegoVisualIDRegistry
					.isCompartmentVisualID(visualID);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isSemanticLeafVisualID(int visualID) {
			return lego.diagram.part.LegoVisualIDRegistry
					.isSemanticLeafVisualID(visualID);
		}
	};

}
